#include"processBar.h"
#include <string.h>
#include <unistd.h>

const char *lable = "|/-\\";
char bar[NUM];
//void processbar(int speed)
//{
  //char bar[NUM];
  //memset(bar,'\0',sizeof(bar));
  //int cnt = 0;
  //while(cnt <= 100)
  //{
    //printf("[%-100s][%d%%][%c]\r",bar,cnt,lable[cnt%4]);
    //bar[cnt++] = STYLE;
    //if(cnt<100) bar[cnt] = '>';
    //fflush(stdout);
    //usleep(speed);
  //}
  //printf("\n");
//}

typedef void(*cb)(int);

void processbar(int rate)
{
  if(rate<0 || rate >100) return;
  printf("[%-100s][%d%%][%c]\r",bar,rate,lable[rate%4]);
  fflush(stdout);
  bar[rate++] = STYLE;
  if(rate<100) bar[rate] = '>';
}


void download(cb cb1)
{
  int total = 1000;//1000MB
  int curr = 0;
  while(curr<=total)
  {
    usleep(50000);
    int rate = curr*100/total;
    cb1(rate);
    curr+=10;
  }
  memset(bar,'\0',sizeof(bar));
  printf("\n");
} 
